<?php
class UsersController extends AppController {
	public $helpers = array (
			'Html',
			'Form',
			'Session'
	);
	
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('register', 'logout', 'index');
	}
// 	public function login(){
// 		if($this->Session->check('userLogin')){
// 			$this->Session->setFlash ('You are already logged in!', 'default', array (
// 					'class' => 'alert alert-danger'));
// 			return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
// 		} 
		
// 		if($this->request->is('post')){
// 			if(!$this->request->data['username']){
// 				$this->Session->setFlash ('Please enter username!', 'default', array (
// 						'class' => 'alert alert-danger'));
// 				return;
// 			}
			
// 			if(!$this->request->data['password']){
// 				$this->Session->setFlash ('Please enter password!', 'default', array (
// 						'class' => 'alert alert-danger'));
// 				return;
// 			}
			
// 			$username = $this->request->data['username'];
// 			$password = $this->request->data['password'];
			
// 			$user = $this->User->find('first', array (
// 				'conditions' => array ('User.username' => $username)));
// // 			echo "<pre>";
// // 			print_r($user);
// // 			exit;
// 			if(!$user){
// 				$this->Session->setFlash ('Username or password is incorrect!', 'default', array (
// 						'class' => 'alert alert-danger'));
// 				return;
// 			}
			
// 			if($user['User']['password'] != $password){
// 				$this->Session->setFlash ('Username or password is incorrect!', 'default', array (
// 						'class' => 'alert alert-danger'));
// 				return;
// 			}else{
// 				$this->Auth->login($user);
// 				$this->Session->write('userLogin', $user);
// 				$this->Session->setFlash ('Login success! Welcome '.$user['User']['fullname'].'!', 'default', array (
// 						'class' => 'alert alert-success'));
// 				return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
// 			}			
// 		}
// 	}

	public function login(){
		if($this->request->is('post')){
			if($this->Auth->login()){
				return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
			}else{
				$this->Session->setFlash ('Username or password is incorrect!', 'default', array (
						'class' => 'alert alert-danger'));
			}
		}
	}
	
	public function logout() {
		return $this->redirect($this->Auth->logout());
	}
	
// 	public function logout(){
// 		if(!$this->Session->check('userLogin')){
// 			$this->Session->setFlash ('You are not logged in. Please login again!', 'default', array (
// 					'class' => 'alert alert-danger'));
// 			return $this->render("/users/login");
			
// 		}else{
// 			$this->Session->delete('userLogin');
// 			$this->Session->setFlash ('Logout success!', 'default', array (
// 					'class' => 'alert alert-success'));
// 			return $this->redirect($this->Auth->logout());
// // 			return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
// 		}
// 	}
	
// 	public function login() {
		
// 		if ($this->request->is('post')) {
// 			if ($this->Auth->login($this->request->data)) {
// // 				print_r($this->request->data);
// // 				exit;
// // 				echo $this->element('sql_dump');
// // 				exit;
// 				return $this->redirect($this->Auth->redirect());
// // 				return $this->redirect($this->Auth->redirectUrl());
// 				// Prior to 2.3 use
// 				// `return $this->redirect($this->Auth->redirect());`
// 			} else {
// 				$this->Session->setFlash(
// 						__('Username or password is incorrect'),
// 						'default',
// 						array(),
// 						'auth'
// 				);
// 			}
// 		}
// 	}
	
	
	/**
	 * Method register
	 */
	public function register() {
		//check login
		if($this->Session->check('userLogin')){
			$this->Session->setFlash ('You already have an account!!', 'default', array (
					'class' => 'alert alert-danger'));
			return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
		} 
		
		//validate input
		if($this->request->is('post')) {
			$username = $this->request->data['username'];
			$admin = $this->User->find('first',array('conditions' => array('User.username' => $username)));
			if($admin) {
				$this->Session->setFlash('Username had been used!!!','default',array('class'=>'alert alert-warning'));
				return $this->render('/users/register');
			}
			$pass = $this->request->data['password'];
			$confirm = $this->request->data['confirmPassword'];
			if($pass != $confirm) {
				$this->Session->setFlash('Confirm password is not correct!!!','default',array('class'=>'alert alert-warning'));
				return $this->render('/users/register');
			}
			//save to db
			$this->User->set(array(
					'username' => $this->request->data['username'],
					'password' => AuthComponent::password($this->request->data['password']),
					'fullname' => $this->request->data['fullname']
			));
			if($this->User->save()) {
				$this->Session->setFlash('Register successfully.','default',array('class'=>'alert alert-success'));
				return $this->redirect(array('action' => 'doLoginAfterRegis', $this->request->data['username']));
			}
			$this->Session->setFlash('Register failed!!!','default',array('class'=>'alert alert-warning'));
			return $this->render('/users/register');
		}
	}
	
	/**
	 * Method login after register
	 */
	public function doLoginAfterRegis($username = null) {
		//check params
		if(!$username) {
			$this->Session->setFlash('Invalid transaction!!!','default',array('class'=>'alert alert-warning'));
			return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
		}
		//initiate session for logged-in
		$this->Session->write('userLogin',$this->User->findByUsername($username));
		return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
	}
}